<template>
  <el-card class="template-container">
    <template #header>
      <div class="header">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">添加</el-button>
        <el-popconfirm title="确定删除吗？" @confirm="handleDelete">
          <template #reference>
            <el-button type="danger" size="small" icon="el-icon-delete">批量删除</el-button>
          </template>
        </el-popconfirm>
      </div>
    </template>
    <el-table v-loading="loading" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="title" label="标题">
      </el-table-column>
      <el-table-column prop="type" label="类型" width="120">
      </el-table-column>

      <el-table-column prop="host" label="host" width="120">
      </el-table-column>

      <el-table-column prop="port" label="port" width="120">
      </el-table-column>

      <el-table-column label="操作" width="220">
        <template #default="scope">
          <a style="cursor: pointer; margin-right: 10px" @click="handleEdit(scope.row.id)">修改</a>

          <el-popconfirm title="确定删除吗？" @confirm="handleDeleteOne(scope.row.id)">
            <template #reference>
              <a style="cursor: pointer">删除</a>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination background layout="prev, pager, next" :total="total" :page-size="pageSize"
      :current-page="currentPage" @current-change="changePage" />
    <DialogAddDb ref='addDb' :reload="getDataList" :type="type" />
  </el-card>
</template>

<script lang="ts">
  import DialogAddDb from '@/components/DialogAddDb.vue'
  import {
    onMounted,
    onUnmounted,
    reactive,
    ref,
    toRefs
  } from 'vue'
  import {
    useRoute,
    useRouter
  } from 'vue-router'
  import * as db_context from '@/models/db_context'

  import {
    ElMessage
  } from 'element-plus'



  export default {
    name: 'DB',
    components: {
      DialogAddDb
    },
    setup() {

      const addDb = ref < any > (null)

      const multipleTable = ref(null)
  
      const state = reactive({
        loading: false,
        tableData: [] as any[], // 数据列表
        multipleSelection: [], // 选中项
        total: 0, // 总条数
        currentPage: 1, // 当前页
        pageSize: 10, // 分页大小
        type: 'add', // 操作类型
        level: 1,
        parent_id: 0
      })

      onMounted(() => {
        getDataList()
      })

      // 获取数据列表
      const getDataList = () => {
        db_context.GenDb
          .getList < db_context.GenDb > ()
          .then(results => {
            console.log('resultsxxxxx:', results);
            state.tableData = results;
          });
      }

      // 添加分类
      const handleAdd = () => {
        state.type = 'add' // 传入弹窗组件用于弹窗 title 判断
        addDb.value.open()
      }
      // 修改分类
      const handleEdit = (id: string) => {
        state.type = 'edit' // 传入弹窗组件用于弹窗 title 判断
        addDb.value.open(id)
      }


      // 选择项
      const handleSelectionChange = () => {
        console.log('handleSelectionChange');
      }
      // 批量删除
      const handleDelete = () => {
        console.log('handleDelete');

      }
     
      // 单个删除
      const handleDeleteOne = async (id: string) => {
        await db_context.GenDb.deleteById(Number.parseInt(id));
       // state.code = '';
        ElMessage.success('删除成功');
        getDataList();

      }


      return {
        ...toRefs(state),
        multipleTable,
        handleAdd,
        handleEdit,
        handleSelectionChange,
        handleDelete,
        handleDeleteOne,
        getDataList,
        addDb


      }
    }
  }
</script>

<style scoped>
  .template-container {
    min-height: 100%;
  }

  .el-card.is-always-shadow {
    min-height: 100% !important;
  }
</style>